package com.apotik.legenda.frameinternal;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.apotik.legenda.custom.TableModelCustom;
import com.apotik.legenda.dao.DoctorDao;
import com.apotik.legenda.dialog.AddEditDokterDialog;
import com.apotik.legenda.model.Doctor;
import com.apotik.legenda.model.User;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.ietf.jgss.MessageProp;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.VetoableChangeListener;
import java.awt.event.KeyAdapter;

public class DokterInternalFrame extends JInternalFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JButton btnAdd;
	private JButton btnEdit;
	private JButton btnDelete;
	private JButton btnRefresh;
	
	private TableModelCustom tableModel;
	private JTable table;
	
	private User user;
	private int modelRow  = -1;
	private JTextField textField;
	
	public DokterInternalFrame(User user) {
		super("Dokter", true, true, true, true);
		setSize(800, 400);
		setVisible(true);
		
		this.user = user;
		
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BorderLayout());
		getContentPane().add(northPanel, BorderLayout.NORTH);
		
		JPanel headPanel = new JPanel();
		headPanel.setLayout(new BorderLayout());
		northPanel.add(headPanel, BorderLayout.NORTH);
		
		JPanel btns1Panel = new JPanel();
		btns1Panel.setLayout(new FlowLayout());
		headPanel.add(btns1Panel, BorderLayout.WEST);
		
		btnAdd = new JButton("Tambah");
		btnAdd.addActionListener(this);
		btnAdd.setMnemonic(KeyEvent.VK_N);
		btnAdd.setToolTipText("ALT + N");
		btns1Panel.add(btnAdd);
		
		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(this);
		btns1Panel.add(btnEdit);
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(this);
		btns1Panel.add(btnDelete);
		
		btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(this);
		btns1Panel.add(btnRefresh);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@SuppressWarnings("unchecked")
			@Override
			public void keyPressed(KeyEvent e) {
				DoctorDao dao = new DoctorDao();
		        if (e.getKeyCode()==KeyEvent.VK_ENTER){
					List<Doctor> list = dao.search(textField.getText());
					System.out.println(list.size());
					if(list.size()>0){
						 deleteAllTableRow();
						 for(Doctor d: list){
							 @SuppressWarnings("rawtypes")
							Vector v = new Vector();
							v.add(d.getDoctorCode());
							v.add(d.getFullName());
							v.add(d.getPhoneNumber()!=null ? d.getPhoneNumber():"");
							v.add(d.getEmail()!=null ? d.getEmail():"");
							v.add(d.isActive()?"Aktif":"Tidak Aktif");
							v.add(d);
							tableModel.insertData(v); 
						 }
					}   
		        }
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
		headPanel.add(textField, BorderLayout.EAST);
		textField.setColumns(25);
		
		JLabel lblCariNama = new JLabel("Cari Nama / Kode Dokter : ");
		lblCariNama.setHorizontalAlignment(SwingConstants.RIGHT);
		headPanel.add(lblCariNama, BorderLayout.CENTER);
		
		/*JPanel btns2Panel = new JPanel();
		btns2Panel.setLayout(new FlowLayout());
		headPanel.add(btns2Panel, BorderLayout.EAST);
		
		searchField = new JTextField();
		searchField.setColumns(14);
		btns2Panel.add(searchField);*/
		
		JSeparator separator = new JSeparator();
		northPanel.add(separator, BorderLayout.SOUTH);
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		String[] columnNames = {"Kode Dokter", "Nama Dokter", "Handphone", "Email", "Is Active"};
		tableModel = new TableModelCustom(columnNames);
		loadDataTable();
		
		table = new JTable();
		table.setModel(tableModel);
		table.setEnabled(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				int viewRow = table.getSelectedRow();
				if(viewRow>-1){
					modelRow = table.convertRowIndexToModel(viewRow);
				}
			}
		});
		scrollPane.setViewportView(table);
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		Object obj =  ae.getSource();
		
		if(obj==btnAdd){
			addNew();
		}else if(obj==btnEdit){
			editItem();
		}else if(obj==btnDelete){
			deleteItem();
		}else if(obj==btnRefresh){
			deleteAllTableRow();
			loadDataTable();
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void loadDataTable(){
		DoctorDao dao = new DoctorDao();
		List<Doctor> list = dao.listByActive(true);
		if(list.size()>0){
			for(Doctor d : list){
				Vector v = new Vector();
				v.add(d.getDoctorCode());
				v.add(d.getFullName());
				v.add(d.getPhoneNumber()!=null ? d.getPhoneNumber():"");
				v.add(d.getEmail()!=null ? d.getEmail():"");
				v.add(d.isActive()?"Aktif":"Tidak Aktif");
				v.add(d);
				tableModel.insertData(v);
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
	private Doctor selectedModel(){
		Doctor doctor = null;
		if(modelRow!=-1){
			Vector v = (Vector) tableModel.getRowVector().get(modelRow);
			if(v!=null){
				doctor = (Doctor) v.get(v.size()-1);
				if(doctor!=null){
					//System.out.println("OO: "+doctor.getDoctorId());
				}
			}
		}
		return doctor;
	}
	
	private void addNew(){
		AddEditDokterDialog dialog = new AddEditDokterDialog("Tambah", tableModel, user);
		dialog.setVisible(true);
		dialog.requestFocus();
	}
	
	private void editItem(){
		if(selectedModel()!=null){
			AddEditDokterDialog dialog = new AddEditDokterDialog("Edit", tableModel, user);
			dialog.setVisible(true);
			dialog.requestFocus();
			dialog.btnSaveSetText("Update");
			
			
			Doctor doctor = selectedModel();
			dialog.fillInTheFields(modelRow, doctor);
		}else{
			JOptionPane.showMessageDialog(null, "Silahkan Pilih Row Table", "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void deleteItem(){
		if(selectedModel()!=null){
			int result = JOptionPane.showConfirmDialog(null, "Apakah Anda yakin ingin menghapusnya?", "Question", JOptionPane.YES_NO_OPTION);
			if(result==JOptionPane.YES_OPTION){
				DoctorDao dao = new DoctorDao();
				boolean isSucces = dao.delete(selectedModel());
				if(isSucces){
					tableModel.removeRow(modelRow);
				}
			}
		}else{
			JOptionPane.showMessageDialog(null, "Silahkan Pilih Row Table", "Exception", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void deleteAllTableRow(){
		tableModel.removeRowAll();
	}
}